<?
/*! \brief Validates an OpenBT service XML against hash
 * 
 */
class Validator
{
	protected $_service, $_hash, $_valid;
	
	function __construct($service, $hash)
	{
		$this->_service = $service;
		$this->_hash = $hash;
		$this->validate();
	}
	
	protected function validate()
	{
		$this->_valid = false;
		
		//INPUTS
		if(isset($this->_service->getSimple()->inputs))
		{
			foreach($this->_service->getSimple()->inputs->input as $input)
			{
				$index = str_replace("-", "", $input->qualifier);
				if(isset($this->_hash[''.$index.'']))
					$input->value = $this->_hash[''.$index.''];
						
				switch($input->type)
				{		
					case 'file':
						if($input->required == "true" 
							&& ! is_uploaded_file($_FILES[$index]['tmp_name']))
							return; //error.
						break;
					case 'argument':
						if($input->required == "true" && $input->value == "")
							return; //error	
						break;
					case 'flag':
						if($this->_hash[''.$index.''] == 1)
							$input->enabled = "true";
						break;
					default:
						break;
				}
			}
		}
		
		//if we made it here, we're valid
		$this->_valid = true;
	}
	
	public function isValid()
	{
		return $this->_valid;
	}
}
